import { request } from "@/utils/request";
import type {
  LoginParams,
  LoginResult,
  MenuTree,
  PasswordParams,
  ResetPasswordParams,
  SendEmailParams,
  UserInfo,
  VerifyEmailParams,
} from "./typing";
import type { ResponseBody } from "../typing";

/**登录认证 */
export const login = (params: LoginParams) => {
  return request.post<LoginParams, ResponseBody<LoginResult>>(
    "/auth/login",
    params
  );
};

/**刷新认证 */
export const refresh = () => {
  return request.post<null, ResponseBody<LoginResult>>("/auth/refresh");
};

/**获取用户信息 */
export const getUserInfo = () => {
  return request.get<null, ResponseBody<UserInfo>>("/auth/userInfo");
};

/**获取用户菜单树信息 */
export const getMenuTree = () => {
  return request.get<null, ResponseBody<MenuTree>>("/auth/menuTree", {
    timeout: 60 * 1000,
  });
};

/**退出登录 */
export const logout = () => {
  return request.post<null, ResponseBody>("/auth/logout");
};

/**切换角色 */
export const switchRole = (roleId: string) => {
  return request.put<string, ResponseBody>("/auth/switch-role", { roleId });
};

/**切换组织 */
export const switchOrganization = (organizationId: string) => {
  return request.put<string, ResponseBody>("/auth/switch-organization", {
    organizationId,
  });
};

/**修改密码 */
export const updatePassword = (params: PasswordParams) => {
  return request.put<PasswordParams, ResponseBody>(
    "/auth/password/update",
    params
  );
};

/**发送邮箱验证码 */
export const sendEmailCode = (params: SendEmailParams) => {
  return request.post<SendEmailParams, ResponseBody>(
    "/auth/email/send",
    params
  );
};

/**验证邮箱验证码 */
export const verifyEmailCode = (params: VerifyEmailParams) => {
  return request.post<VerifyEmailParams, ResponseBody>(
    "/auth/email/verify",
    params
  );
};

/**重置密码 */
export const resetPassword = (params: ResetPasswordParams) => {
  return request.put<ResetPasswordParams, ResponseBody>(
    "/auth/password/reset",
    params
  );
};
